import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class MainPanel extends JFrame {
    private final int WIDTH = 600;
    private final int HEIGHT = 600;
    public static final int TextFieldW = 40;
    public static final int TextFieldH = 30;
    private static Container container;
    private static JTextArea textArea;

    public MainPanel() {
        super( "Supply Chain Management" );

        container = getContentPane();
        container.setLayout( new BorderLayout() );

        //JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;

        // set up menu
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menu = new JMenu("Display");
        menuBar.add(menu);

        menuItem = new JMenuItem("Product");
        menu.add(menuItem);

         menuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                textArea.setText("");
                textArea.append("List of products: " + '\n');
                for (int i = 0; i < SupplyChainManagement.p.size(); ++i) {
                    textArea.append((i+1) + ". " + SupplyChainManagement.p.get(i).getNameProduct()+ '\n');
                }
                //SupplyChainManagement.displayProduct(textArea);
            }
        });

        // submenu
        submenu = new JMenu("Inventory");
        menu.add(submenu);

        menuItem = new JMenuItem("Inventory for each product");
        submenu.add(menuItem);

        menuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                textArea.setText("");
                for (int i = 0; i < SupplyChainManagement.p.size(); ++i) {
                    textArea.append(SupplyChainManagement.p.get(i).getNameProduct() + ": " + '\n');
                    ArrayList<Ingredients> temp = SupplyChainManagement.p.get(i).getRawMeterials();
                    ArrayList amount = SupplyChainManagement.p.get(i).getNumRMNeeded();

                    for (int j = 0; j < temp.size(); ++j) {
                        textArea.append("   " + temp.get(j).getNameProduct() + ": " + amount.get(j) + '\n');
                    }
                    textArea.append(" " + '\n');
            }
                //SupplyChainManagement.displayInventory(textArea);
            }
        });

        menuItem = new JMenuItem("Inventory On Hand");
        submenu.add(menuItem);

        menuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                textArea.setText("");
                for (int i = 0; i < SupplyChainManagement.inv.size(); ++i) {
                    int num = SupplyChainManagement.inv.get(i).getInvOnHand();
                    if (num >= 0) {
                        textArea.append(SupplyChainManagement.inv.get(i).getNameProduct() + ": " + num + '\n');
                    }
                }
                //SupplyChainManagement.currentInv(textArea);
            }
        });

        menu = new JMenu("Add");
        menuBar.add(menu);

        menuItem = new JMenuItem("Product");
        menu.add(menuItem);

        menuItem.addActionListener(addNewProduct());    // add new product

        menuItem = new JMenuItem("Inventory");
        menu.add(menuItem);

        menuItem.addActionListener(addNewInventory()); // add new inventory
        
        menu = new JMenu("Produce");
        menuBar.add(menu);

        menuItem = new JMenuItem("Produce Product");
        menu.add(menuItem);

        menuItem.addActionListener(produceProduct());

        menu = new JMenu("Connect");
        menuBar.add(menu);

        menuItem = new JMenuItem("Connect to Server");
        menu.add(menuItem);

        menuItem.addActionListener(connectServer());
        
        // text area
        textArea = new JTextArea(TextFieldH, TextFieldW);
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);
        container.add(scrollPane);
        
      
        setSize(WIDTH,HEIGHT);
        setVisible( true );
    }

    ActionListener produceProduct() {
        return new produceHandler();
    }

    class produceHandler implements ActionListener {
        JDialog dialog;
        public void actionPerformed (ActionEvent evt) {
            textArea.setText("");
            if (dialog == null) {
                dialog = new ProduceDialog(MainPanel.this);
            }
            dialog.show();
        }
    }

    ActionListener addNewProduct() {
        return new addProductHandler();
    }

    class addProductHandler implements ActionListener {
        JDialog dialog;
        public void actionPerformed (ActionEvent evt) {
            textArea.setText("");
            if (dialog == null) {
                dialog = new AddProductDialog(MainPanel.this);
            }
            dialog.show();
        }
    }

    ActionListener addNewInventory() {
        return new addInventoryHandler();
    }

    class addInventoryHandler implements ActionListener {
        JDialog dialog;
        public void actionPerformed (ActionEvent evt) {
            textArea.setText("");
            if (dialog == null) {
                dialog = new AddInventoryDialog(MainPanel.this);
            }
            dialog.show();
        }
    }

    ActionListener connectServer() {
        return new networkHandler();
    }

    class networkHandler implements ActionListener {
         public void actionPerformed (ActionEvent evt) {
            //JDialog server, client;
            textArea.setText(""); 
            
            Client client = new Client(MainPanel.this);
            //Server server = new Server(MainPanel.this);
            //server.start();
            //textArea.append(server.getmessage());
            
            //client.runClient();
            //server.runServer();
        }
    }
}
